#!/usr/bin/env python
##################################################
#   This program is design to apply DA algorithm #
#   on MapReduce programming model               #
#   We use the in-memory database as the data ex-#
#   change channel                               #
##################################################

import sqlite3
import copy
import networkx as nx

''' 
base class of DA algorithm for graph computation
based on vertex-centric.
'''
class Base_DA:
    # when delta=1, it is typical bsp model
    def __init__(self, name, delta = 1):
        self.name = name
        self.delta = delta
        self.intermediate_result = dict()

    # computing
    def computing(self):
        pass

# PageRank algorithm inherit from Base_DA
class PageRank(Base_DA):
    def __init__(self, delta = 1, subgraph):
        Base_DA.__init__("pr", delta)
        self.subgraph = subgraph

    def computing(self):
        for node, d in graph.nodes(data = True):
            if len(graph.out_edges(node)) <= 0:
                continue
            share_pr = d['weight'] / len(graph.out_edges(node))
            for u, v in graph.out_edges(node):
                intermediate_result[v] = share_pr
        pass

# Runtime of DA algorithms
class Runtime:
    def __init__(self, algo, delta = 1, tolerant = 0.001, round_num = 100):
        self.algo = algo
        self.delta = delta
        self.tolerant = tolerant
        self.round_num = round_num
        pass

    def start(self):
        while loss > self.tolerant and rounds < self.round_num:
            self.algo.computing()
            if rounds % self.delta == 0:
                self.globalsynch(algo.intermediate_result)
            else:
                self.localsynch(algo.intermediate_result)
            pass
        pass

    # exchange data within local boundary
    def localsend(self, dest, data):
        pass

    # exchange data across nodes
    def globalsend(self, dest, data):
        pass

    # local data exchange
    def local_exchange(self):
        pass

    # global data exchange 
    def global_exchange(self):
        pass

# for test use
if __name__ == "__main__":
    pagerank = PageRank()
    rt = Runtime(pagerank)
    pass
